package com.projectpractice.service;

import com.projectpractice.bean.Article;
import com.projectpractice.common.utils.Md5Util;
import com.projectpractice.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleRepository articleRepository;

    @Override
    public List<Article> getArticles(){
        return articleRepository.findAll();
    }

    @Override
    public int delete(Integer id) {
        try{
            articleRepository.deleteById(id);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public Article regist(Article article) {
        return articleRepository.save(article);
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleRepository.getArticleByAid(id);
    }

    @Override
    public int update(Article a) {
        articleRepository.updateById(a.getAid(),a.getAcreatetime(),a.getTitle(),a.getCid(),a.getScontent(),a.getAstate());
        return 1;
    }
}
